﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DotNetX
{
    public class FormChecker
    {
        #region Properties

        public bool HasErrors
        {
            get { return _hasErrors; }
            private set { _hasErrors = value; }
        }
        private bool _hasErrors = false;

        public List<string> Errors
        {
            get { return _errors; }
        }
        private List<string> _errors = new List<string>();

        public string ErrorsByBRTag
        {
            get
            {
                string s = String.Empty;

                foreach (string e in this.Errors)
                {
                    s = String.Format("{0}{1}<br />",
                        s, e);
                }

                return s;
            }
        }

        #endregion

        #region Public Methods

        #region Errors

        public void AddError(string error)
        {
            this.Errors.Add(error);
            this.HasErrors = true;
        }

        #endregion

        #region Strings

        public string GetString(string value, string propertyName)
        {
            if (String.IsNullOrEmpty(value))
            {
                this.AddStringError(propertyName);
                return String.Empty;
            }

            return value;
        }

		#region Versions

		public string GetVersion(string value, string propertyName)
		{
			Version v;
			bool res = Version.TryParse(value, out v);

			if (!res)
			{
				this.AddVersionError(propertyName);
				return String.Empty;
			}

			return v.ToString();
		}

        #endregion

        #region HttpPostedFile

        public HttpPostedFileBase GetFile(HttpContextBase context, string controlName, string propertyName)
        {
            HttpPostedFileBase file = null;

            foreach (string f in context.Request.Files)
            {
                if (f.ToLower().Trim() == controlName.ToLower().Trim())
                {
                    file = context.Request.Files[f];
                }
            }

            if (file == null || file.ContentLength <= 0)
            {
                this.AddFileError(propertyName);
            }

            return file;
        }

        #endregion

        #endregion

		#endregion

		#region Private Methods

		#region Strings

		private void AddStringError(string propertyName)
        {
            this.HasErrors = true;

            this.Errors.Add(String.Format("Please enter a value for '{0}'.", propertyName));
        }

		#endregion

		#region Version

		private void AddVersionError(string propertyName)
		{
			this.HasErrors = true;

			this.Errors.Add(String.Format("Please enter a valid version (x.y.z.w) for '{0}'.", propertyName));
		}

		#endregion

        #region HttpPostedFile

        private void AddFileError(string propertyName)
        {
            this.HasErrors = true;

            this.Errors.Add(String.Format("Please upload a file for '{0}'.", propertyName));
        }

        #endregion

        #endregion
    }
}
